<?php
$NRIC = $_REQUEST['NRIC'];
$customerName = $_REQUEST['CustomerName'];
$citizenship = $_REQUEST['Citizenship'];
$beforeDeviation = intval($_REQUEST['BeforeDeviation']);
$afterDeviation = intval($_REQUEST['AfterDeviation']);
$dateOfBirth = $_REQUEST['DateOfBirth'];
$gender = $_REQUEST['Gender'];
$centreId = intval($_REQUEST['CentreName']);
$checkNRIC = false;

include 'conn.php';
$sql = "Select * from client where NRIC = '$NRIC'";
$result = mysql_query($sql);
if (mysql_num_rows($result) == 0) {
    if ($dateOfBirth < date('Y-m-d')) {
        if (strlen($NRIC) == 9) {
            if (preg_match('/^[ST][0-9]{7}[JZIHGFEDCBA]$/', $NRIC)) { // NRIC
                $check = "JZIHGFEDCBA"; //"JZIHGFEDCBA","ABCDEFGHIZJ"
            } else if (preg_match('/^[FG][0-9]{7}[XWUTRQPNMLK]$/', $NRIC)) { // FIN
                $check = "XWUTRQPNMLK"; //"XWUTRQPNMLK","KLMNPQRTUWX"
            } else {
                $checkNRIC = false;
            }

            $total = $NRIC[1] * 2 + $NRIC[2] * 7 + $NRIC[3] * 6 + $NRIC[4] * 5 + $NRIC[5] * 4 + $NRIC[6] * 3 + $NRIC[7] * 2;

            if ($NRIC[0] == "T" OR $NRIC[0] == "G") {
                // shift 4 places for after year 2000
                $total = $total + 4;
            }

            if ($NRIC[8] == $check[$total % 11]) {
                $checkNRIC = true;
            } else {
                $checkNRIC = false;
            }

            if ($checkNRIC == true) {
                $sql = "INSERT INTO client (NRIC,CustomerName,Citizenship,BeforeDeviation,AfterDeviation, DateOfBirth,Gender,CentreId) VALUES ('$NRIC','$customerName','$citizenship',$beforeDeviation,$afterDeviation,'$dateOfBirth','$gender','$centreId')";

                $result = @mysql_query($sql);
                if ($result) {
                    echo json_encode(array('success' => true));
                } else {
                    echo json_encode(array('msg' => 'Some Error Occured!'));
                    print '<script type="text/javascript">';
                    print 'alert("Some Error Occured!")';
                    print '</script>';
                }
            } else {
                echo json_encode(array('msg' => 'You have entered an invalid NRIC number.'));
                print '<script type="text/javascript">';
                print 'alert("You have entered a invalid NRIC Number!")';
                print '</script>';
            }
        } else {
            echo json_encode(array('msg' => 'You have entered an invalid NRIC number.'));
            print '<script type="text/javascript">';
            print 'alert("You have entered an invalid NRIC Number!")';
            print '</script>';
        }
    } else {
        echo json_encode(array('msg' => 'The date of birth cannot be today or the future!'));
        print '<script type="text/javascript">';
        print 'alert("The date of birth cannot be today or the future!")';
        print '</script>';
    }
}else{
    echo json_encode(array('msg' => 'The NRIC Exist in the database already!'));
        print '<script type="text/javascript">';
        print 'alert("The NRIC Exist in the database already!")';
        print '</script>';
}
?>